<?php
        require_once('auth.php');
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<HTML>
<HEAD>
<TITLE>Upload REDCAT File</TITLE>
<link href="style.css" rel="stylesheet" type="text/css" />
</HEAD>
<script type="text/javascript">
function newPopup(url) {
        popupWindow = window.open(
        url, 'popUpWindow', 'height=450,width=800,left=50,top=50,resizable=yes,scrollbars=yes,toolbar=no,menubar=no,location=no,directories=no,status=yes')
}
function newPopup2(url) {
        popupWindow = window.open(
        url,'popUpWindow','height=450,width=400,left=50,top=50,resizable=yes,scrollbars=yes,toolbar=no,menubar=no,location=no,directories=no,status=yes')
}
</script>
<BODY>
<center>
<img src="redcat_logo.jpg">
</center>
<div>
<a href="JavaScript:newPopup('account.php');" style="float:left">My Files</a>
<a href="logout.php" style="float:right">Logout</a>
</div><br />

<div>
<a href="JavaScript:newPopup2('help-menu.php');" style="float:left">Click here for help</a>
</div><br /><br />

<H3 align="center">Upload REDCAT data file:</H3>
<H3 align="center">
  <form action="/REDCAT/uploaded.php" method="POST" enctype="multipart/form-data">
  <input type="file" name="redcatfile" id="redcatfile">
  <input type="submit" name="Upload" value="Upload" /></form>
</H3>

<div>
  <form method="GET" action="/REDCAT/run.php"><input type="submit" name="run" id="run" value="Run" style="float:left"></form>

  <form method="GET" action="/REDCAT/error.php"><input type="submit" name="err" id="err" value="Error Analysis" style="float:left"></form>

  <form method="GET" action="/REDCAT/rdc.php"><input type="submit" name="rdc" id="rdc" value="Calculate RDCs" style="float:left"></form>
</div><br /><br />

<style type="text/css">
tr.filelisting:hover {background-color:#FFC }
</style>

<?php

$login = $_SESSION['SESS_LOGIN'];
$currentdir = getcwd();
$target = $currentdir."/workspace/".$login."/";
$target = $target. basename( $_FILES['redcatfile']['name'] );
$ok = 1;
//Exclude executable files from upload
if( $uploaded_type == "text/exe")
{
        echo '<script>alert("No executable files!")</script>';
        $ok = 0;
}
if($ok == 0)
{
        echo '<script>alert("Sorry your file was not uploaded.")</script>';
}
else
{
        //Store file uploaded into user's directory
        if(move_uploaded_file($_FILES['redcatfile']['tmp_name'], $target))
        {
                //Open file for reading into an HTML table
                $_SESSION['SESS_CURR_FILE'] = $target;  
                $file = file($target);
                echo '<H5 align = "center">';
                echo '<table border="1">';
                echo '<tr><th align = "center">Eq.#</th><th align = "center">X1</th><th align = "center">Y1</th><th align = "center">Z1</th><th align = "center">X2</th><th align = "center">Y2</th><th align = "center">Z2</th><th align = "center">Dipol</th><th align = "center">Error</th><th align = "center">Comments</th></tr>';
                $eq = 1;
                foreach($file as $line)
                {
                        $comment = preg_split('(\/\*)', $line);
                        $value = preg_split("/[\s]+/", $comment[0]);
                        echo "<tr class=\"filelisting\"><td>$eq</td><td>$value[0]</td><td>$value[1]</td><td>$value[2]</td><td>$value[3]</td><td>$value[4]</td><td>$value[5]</td><td>$value[6]</td><td>$value[8]</td><td>/*$comment[1]</td></tr>";
                        $eq++;
                }
                echo '</table>';
                echo '</H3>';
        }
        else
        {
                echo '<script>alert("There was an error uploading your file.")</script>';
        }
}
session_write_close();
?>

</HTML>
